import urllib
from urllib.request import urlopen

if __name__ == "__main__":
    url = 'https://www.w3cschool.cn/search?w='  # 编程狮搜索页面
    keyword = "Python 教程"
    key_code = urllib.request.quote(keyword)
    url_all = url + key_code
    header = {
    'User-Agent':'Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
    }
    request = urllib.request.Request(url_all, headers=header)
    response = urllib.request.urlopen(request).read()
    fh = open("./urllib_test_w3cschool_search.html", "wb")  # 将文件写入到当前目录中
    fh.write(response)
    fh.close()